﻿
Imports System.Threading

Namespace slimlock
    Public Structure sequentiallock
        Implements islimlock
        Private assignToken As Int32
        Private token As Int32

        Public Sub wait() Implements islimlock.wait
            Dim tokengot As Int32 = 0
            tokengot = Interlocked.Increment(assignToken) - 1
            'very high race-condition rate
            lazy_wait_when(Function(ByRef x) x < tokengot, token)
        End Sub

        Public Sub release() Implements islimlock.release
            Interlocked.Increment(token)
        End Sub

        Public Function can_thread_owned() As Boolean Implements islimlock.can_thread_owned
            Return True
        End Function

        Public Function can_cross_thread() As Boolean Implements islimlock.can_cross_thread
            Return True
        End Function
    End Structure
End Namespace
